<?php
require('e/class/connect.php'); //引入数据库配置文件和公共函数文件
require('e/class/db_sql.php'); //引入数据库操作文件
require("e/class/q_functions.php");
$link=db_connect();
$action = '';
if(count($_REQUEST) > 0 && $_REQUEST['action']) {
    $action = $_REQUEST['action'];
}
switch ($action){
    case 'getList':
        getList();
        break;
    case 'getDetail':
        getDetail();
        break;
    case 'getComment':
        getComment();
        break;
    case 'addComment':
        addComment();
        break;
    case 'upload':
        upload();
        break;
    case 'add':
        add();
        break;
    default:
        break;
}
function getList(){

    if(count($_GET) > 0) {
        $empire=new mysqlquery();
        $sql = $empire->query("select * from fdc_chuzu where status = 0 order by id desc");
        $list = [];
        while ($r=$empire->fetch($sql))
        {
            $item = [
                'id' => $r['id'],
                'user_id' => $r['user_id'],
                'title' => $r['title'],
                'content' => $r['content'],
                'description' => $r['description'],
                'picurls' => $r['picurls'],
                'status' => $r['status'],
                'created_time' => $r['created_time'],
                'updated_time' => $r['updated_time'],
                'remark' => $r['remark'],
                'view_count' => intval($r['view_count']),
                'publish_date' => intval($r['publish_date']),
                'isLike' => $r['isLike']?true:false,
                'like_count' => intval($r['like_count']),
                'comment_count' => intval($r['comment_count'])

            ];
            $picurls = $item['picurls']?explode(',',$item['picurls']):[];
            if(count($picurls) > 0){
                foreach ($picurls as &$p){
                    $p = 'http://127.0.0.1' . $p;
                }
            }
            $item['picurls'] = $picurls;
            $list[] = $item;
        }
        echo json_encode($list, 256);
    }
}

function getDetail(){

    if(count($_GET) > 0) {
        $id = $_GET['id'];
        $empire=new mysqlquery();
        $r = $empire->fetch1("select * from fdc_chuzu where id = {$id} limit 1");
        $r['publish_date'] = intval($r['publish_date']);
        $picurls = $r['picurls']?explode(',',$r['picurls']):[];
        if(count($picurls) > 0){
            foreach ($picurls as &$p){
                $p = 'http://127.0.0.1' . $p;
            }
        }
        $r['picurls'] = $picurls;
        echo json_encode($r);
    }
}

function getComment(){
    if(count($_GET) > 0) {
        $id = $_GET['id'];
        $empire=new mysqlquery();
        $sql = $empire->query("select * from fdc_comment where fid = {$id} order by id desc");
        $list = [];
        while ($r=$empire->fetch($sql))
        {
            $r['comment_date'] = intval($r['comment_date']);
            $list[] = $r;
        }
        echo json_encode($list, 256);
    }
}

function upload()
{
    if (count($_FILES) > 0) {
        include ECMS_PATH . "e/extend/ueditor/php/Uploader.class.php";

        /* 上传配置 */
        $base64 = "upload";
        $config = array(
            //"pathFormat" =>  ECMS_PATH .'\upload\fdc\{yyyy}{mm}{dd}\{time}{rand:6}',
            "pathFormat" => 'gzhou.news/upload/fdc/{yyyy}{mm}{dd}/{yyyy}{mm}{dd}{hh}{ii}{ss}{rand:6}',
            "maxSize" => 1024 * 1024 * 10, /* 上传大小限制，单位B */
            "allowFiles" => [".png", ".jpg", ".jpeg"],
            "fileurl" => ''
        );
//var_dump($config);
//exit;
        $fieldName = 'file';
        /* 生成上传实例对象并完成上传 */
        $up = new Uploader($fieldName, $config, $base64);

        /**
         * 得到上传文件所对应的各个参数,数组结构
         * array(
         *     "state" => "",          //上传状态，上传成功时必须返回"SUCCESS"
         *     "url" => "",            //返回的地址
         *     "title" => "",          //新文件名
         *     "original" => "",       //原始文件名
         *     "type" => ""            //文件类型
         *     "size" => "",           //文件大小
         * )
         */

        /* 返回数据 */
        $result = json_encode($up->getFileInfo());
        /* 返回数据 */
        echo $result;
    }
}

function addComment(){
    if(count($_POST) > 0) {
        $empire=new mysqlquery();
        $comment_date = time() * 1000;
        $comment_ip = '127.0.0.1';
        $sql = $empire->query("insert into fdc_comment(fid,comment_content,comment_type,comment_date,comment_ip,province) values('{$_POST['fid']}','{$_POST['comment_content']}','{$_POST['comment_type']}',{$comment_date},'{$comment_ip}','{$_POST['province']}');");
        if ($sql) {
            $id = $empire->lastid();
            echo $id;
        }else{
            echo 0;
        }

    }
}

function add(){
    if(count($_POST) > 0) {

        $empire=new mysqlquery();
        $created_time = time();
        $publish_date = $created_time * 1000;
        $sql = $empire->query("insert into fdc_chuzu(user_id,title,content,description,picurls,created_time,publish_date) values(1,'{$_POST['title']}','{$_POST['content']}','{$_POST['description']}','{$_POST['picurls']}',{$created_time},{$publish_date});");
        if ($sql) {
            $id = $empire->lastid();
            echo $id;
        }else{
            echo 0;
        }

    }
}

db_close();
$empire=null;
?>